// 会员相关API

import { $api } from '~/utils/request'

export interface MemberUser {
  id: number
  userName: string
  nickName: string
  email: string
  phonenumber: string
  sex: string
  avatar: string
  vipLevel: number
  vipExpireTime: string
  balance: number
  points: number
  createTime: string
}

export interface Comment {
  id: number
  movieId: number
  userId: number
  userName: string
  userAvatar: string
  content: string
  rating: number
  likeCount: number
  replyCount: number
  parentId?: number
  createTime: string
  replies?: Comment[]
}

export interface Collection {
  id: number
  movieId: number
  movieTitle: string
  moviePoster: string
  createTime: string
}

export interface WatchHistory {
  id: number
  movieId: number
  movieTitle: string
  moviePoster: string
  episodeNumber?: number
  watchTime: number // 观看时长(秒)
  totalTime: number // 总时长(秒)
  progress: number // 进度百分比
  lastWatchTime: string
}

export interface Order {
  id: number
  orderNo: string
  amount: number
  payType: string
  status: number
  productName: string
  createTime: string
  payTime?: string
}

// 获取用户信息
export const getUserInfo = (userId: number) => {
  return $api.get<MemberUser>(`/app/member/info/${userId}`)
}

// 更新用户信息
export const updateUserInfo = (data: Partial<MemberUser>) => {
  return $api.put('/app/member/info', data)
}

// 获取视频评论列表
export const getCommentList = (videoId: number, isDm?: string, pageNum: number = 1, pageSize: number = 20) => {
  return $api.get<{
    rows: Comment[]
    total: number
  }>(`/app/member/comments/video/${videoId}`, { isDm, pageNum, pageSize })
}

// 获取弹幕列表
export const getDanmakuList = (videoId: number) => {
  return $api.get<Comment[]>(`/app/member/danmaku/video/${videoId}`)
}

// 发表评论/弹幕（从JWT token获取userId）
export const addComment = (data: {
  videoId: number
  content: string
  isDm?: string
  start?: number
}) => {
  return $api.post('/app/member/comment', data)
}

// 删除评论
export const deleteComment = (commentId: number) => {
  return $api.delete(`/app/member/comment/${commentId}`)
}

// 获取用户收藏列表（从JWT token获取userId）
export const getCollectionList = (pageNum: number = 1, pageSize: number = 20) => {
  return $api.get<{
    rows: Collection[]
    total: number
  }>('/app/member/collections', { pageNum, pageSize })
}

// 添加收藏（短视频）
export const addCollection = (shortId: number) => {
  return $api.post('/app/member/collection', { shortId })
}

// 取消收藏
export const removeCollection = (collectionId: number) => {
  return $api.delete(`/app/member/collection/${collectionId}`)
}

// 检查是否收藏（从JWT token获取userId）
export const checkCollection = (shortId: number) => {
  return $api.get<boolean>('/app/member/collection/check', { shortId })
}

// 点赞短视频（从JWT token获取userId）
export const likeShort = (shortId: number) => {
  return $api.post('/app/member/like', { shortId })
}

// 检查是否点赞（从JWT token获取userId）
export const checkLike = (shortId: number) => {
  return $api.get<boolean>('/app/member/like/check', { shortId })
}

// 对影视评分（从JWT token获取userId）
export const addRate = (data: { movieBasicsId: number, rate: number }) => {
  return $api.post('/app/member/rate', data)
}

// 获取影视的用户评分（从JWT token获取userId）
export const getMovieRate = (movieBasicsId: number) => {
  return $api.get(`/app/member/rate/movie/${movieBasicsId}`)
}

// 获取影视的平均评分
export const getMovieAvgRate = (movieBasicsId: number) => {
  return $api.get<{ avgRate: number, rateCount: number }>(`/app/member/rate/movie/${movieBasicsId}/avg`)
}

// ==================== 以下功能暂未实现 ====================

// // 获取观看历史
// export const getWatchHistory = (pageNum: number = 1, pageSize: number = 20) => {
//   return $api.get<{
//     rows: WatchHistory[]
//     total: number
//   }>('/member/history/list', { pageNum, pageSize })
// }

// // 添加观看记录
// export const addWatchHistory = (data: {
//   movieId: number
//   episodeNumber?: number
//   watchTime: number
//   totalTime: number
// }) => {
//   return $api.post('/member/history/add', data)
// }

// // 更新观看进度
// export const updateWatchProgress = (historyId: number, progress: number) => {
//   return $api.put('/member/history/progress', { historyId, progress })
// }

// // 删除观看记录
// export const removeWatchHistory = (historyId: number) => {
//   return $api.delete(`/member/history/${historyId}`)
// }

// // 获取订单列表
// export const getOrderList = (pageNum: number = 1, pageSize: number = 20) => {
//   return $api.get<{
//     rows: Order[]
//     total: number
//   }>('/member/order/list', { pageNum, pageSize })
// }

// // 创建订单
// export const createOrder = (data: {
//   productType: string
//   productId: number
//   amount: number
// }) => {
//   return $api.post('/member/order/create', data)
// }

// // 支付订单
// export const payOrder = (orderNo: string, payType: string) => {
//   return $api.post('/member/order/pay', { orderNo, payType })
// }

// // 获取钱包信息
// export const getWalletInfo = () => {
//   return $api.get<{
//     balance: number
//     points: number
//     vipLevel: number
//     vipExpireTime: string
//   }>('/member/wallet/info')
// }

// // 充值
// export const recharge = (amount: number, payType: string) => {
//   return $api.post('/member/wallet/recharge', { amount, payType })
// }